package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

//  相当于一个配置类
@Configuration
public class CorsConfig {

    //  设置跨域：
    @Bean
    public WebFilter webFilter(){

        //  CorsConfiguration
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");        //  设置域名
        corsConfiguration.setAllowCredentials(true);    //  携带cookie
        corsConfiguration.addAllowedMethod("*");        //  设置方法
        corsConfiguration.addAllowedHeader("*");        //  设置请求头

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        //  返回数据：
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
